package com.example.demo.controller.userInfo;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.example.demo.controller.BaseController;
import com.example.demo.domain.AjaxResult;
import com.example.demo.domain.userInfo.UserInfo;
import com.example.demo.service.role.RoleService;
import com.example.demo.service.userInfo.UserInfoService;
import com.example.demo.web.TableDataInfo;


@Controller
@RequestMapping("/userInfo")
public class UserInfoController extends BaseController{

	    @Autowired
	    private UserInfoService userInfoService;

	    @Autowired
	    private RoleService roleService;

//	    @RequiresPermissions("system:user:view")
	    @GetMapping()
	    public String user()
	    {
	        return  "userInfo/userInfo";
	    }

//	    @RequiresPermissions("system:user:list")
	    @PostMapping("/list")
	    @ResponseBody
	    public TableDataInfo list(UserInfo user)
	    {
	    	startPage();
	        List<UserInfo> list = userInfoService.selectUserList(user);
	        return getDataTable(list);
	    }
	    /**
	     * 新增用户
	     */
	    @GetMapping("/add")
	    public String add(ModelMap mmap)
	    {
	        mmap.put("roles", roleService.selectRoleAll());
	        
	        return  "/userInfo/add";
	    }

	    /**
	     * 新增保存用户
	     */
//	    @RequiresPermissions("system:user:add")
	 
	    @PostMapping("/add")
	    @Transactional(rollbackFor = Exception.class)
	    @ResponseBody
	    public AjaxResult addSave(UserInfo userInfo)
	    {
	    	
	        if (userInfo.getUid()!=null && UserInfo.isAdmin(userInfo.getUid()))
	        {
	            return error("不允许修改超级管理员用户");
	        }
	        return toAjax(userInfoService.insert(userInfo));
	    }

	    /**
	     * 修改用户
	     */
	    @GetMapping("/edit/{uid}")
	    public String edit(@PathVariable("uid")Integer uid, ModelMap mmap)
	    {
	        mmap.put("userInfo", userInfoService.selectByPrimaryKey(uid));
	        mmap.put("roles", roleService.selectRolesByUserId(uid));
	        
	        return  "userInfo/edit";
	    }

	    /**
	     * 修改保存用户
	     */
//	    @RequiresPermissions("system:user:edit")
	    @PostMapping("/edit")
	    @ResponseBody
	    public AjaxResult editSave(UserInfo user)
	    {
	    	
	        if (user.getUid()!=null && UserInfo.isAdmin(user.getUid()))
	        {
	            return error("不允许修改超级管理员用户");
	        }
	        return toAjax(userInfoService.updateByPrimaryKeySelective(user));
	    }

//	    @RequiresPermissions("system:user:resetPwd")
	    @GetMapping("/resetPwd/{uid}")
	    public String resetPwd(@PathVariable("uid") Integer uid, ModelMap mmap)
	    {
	        mmap.put("user", userInfoService.selectByPrimaryKey(uid));
	        return  "userInfo/resetPwd";
	    }

//	    @RequiresPermissions("system:user:resetPwd")
	    @PostMapping("/resetPwd")
	    @ResponseBody
	    public AjaxResult resetPwd(UserInfo user)
	    {
	        return toAjax(userInfoService.resetUserPwd(user));
	    }

//	    @RequiresPermissions("system:user:remove")
	    @PostMapping("/remove")
	    @ResponseBody
	    public AjaxResult remove(String ids)
	    {
	        try
	        {
	            return toAjax(userInfoService.deleteUserByIds(ids));
	        }
	        catch (Exception e)
	        {
	            return error(e.getMessage());
	        }
	    }

	    /**
	     * 校验用户名
	     */
	    @PostMapping("/checkUserNameUnique")
	    @ResponseBody
	    public int checkLoginNameUnique(UserInfo user)
	    {
	    	
	        return userInfoService.checkUserNameUnique(user.getUsername());
	    }

	    /**
	     * 用户状态修改
	     * @throws Exception 
	     */
//
//	    @RequiresPermissions("system:user:edit")
	    @PostMapping("/changeState")
	    @ResponseBody
	    public AjaxResult changeStatus(UserInfo user) throws Exception
	    {
	        return toAjax(userInfoService.changeState(user));
	    }

}
